export const debounce = (fn: Function, wait: number, immediate: boolean = false) => {
  let timer: number | null = null;
  return function (this: any) {
    let args = arguments;

    if (timer) {
      window.clearTimeout(timer);
    }

    if (immediate && !timer) {
      fn.apply(this, args);
    } 
      
    timer = window.setTimeout(() => {
      fn.apply(this, args);
      timer = null;
    }, wait);
  }
}

export default debounce;